package org.eae.eldf.framework.pager;

import java.io.Serializable;
import java.util.List;

public class Pagination<T> implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 214709616816366738L;
	public static final int NUMBERS_PER_PAGE = 10;
	 //一页显示的记录数
	  private int numPerPage = NUMBERS_PER_PAGE; 
	 //记录总数
	  private int totalRows; 
	 //总页数
	  private int totalPages; 
	 //当前页码
	  private int currentPage = 1; 
	  //起始行数
	  private int startIndex;
	  //结束行数
	  private int lastIndex;
	  //结果集存放List
	  private List<T> resultList;
	  
	  private String orderBy;
	  
	  private String sort;
	 
	    /**
		 * 构造MySQL数据分页SQL 
		 * @param queryString
		 * @param startIndex
		 * @param pageSize
		 * @return
		 */
		public String getMySQLPageSQL(String queryString, Integer startIndex, Integer pageSize)
		{
			String result = "";
			if (null != startIndex && null != pageSize)
			{
				result = queryString + " limit " + startIndex + "," + pageSize;
			} else if (null != startIndex && null == pageSize)
			{
				result = queryString + " limit " + startIndex;
			} else
			{
				result = queryString;
			}
			return result;
		}
		
		
	 
	  public int getCurrentPage() {
	    return currentPage;
	  } 

	  public void setCurrentPage(int currentPage) {
	    this.currentPage = currentPage==0?1:currentPage;
	  }

	  public int getNumPerPage() {
	    return numPerPage;
	  }

	  public void setNumPerPage(int numPerPage) {
	    this.numPerPage = numPerPage;
	  }

	  public List<T> getResultList() {
	    return resultList;
	  }

	  public void setResultList(List<T> resultList) {
	    this.resultList = resultList;
	  }

	  public int getTotalPages() {
	    return totalPages;
	  }
	 //计算总页数
	  public void setTotalPages() {
	    if(totalRows % numPerPage == 0){
	      this.totalPages = totalRows / numPerPage;
	    }else{
	      this.totalPages = (totalRows / numPerPage) + 1;
	    }
	  }

	  public int getTotalRows() {
	    return totalRows;
	  }

	  public void setTotalRows(int totalRows) {
	    this.totalRows = totalRows;
	  }

	  public int getStartIndex() {
	    return startIndex;
	  }

	  public void setStartIndex() {
	    this.startIndex = (currentPage - 1) * numPerPage;
	  }

	  public int getLastIndex() {
	    return lastIndex;
	  }

	 
	 //计算结束时候的索引
	  public void setLastIndex() {
	    if( totalRows < numPerPage){
	      this.lastIndex = totalRows;
	    }else if((totalRows % numPerPage == 0) || (totalRows % numPerPage != 0 && currentPage < totalPages)){
	      this.lastIndex = currentPage * numPerPage;
	    }else if(totalRows % numPerPage != 0 && currentPage == totalPages){//最后一页
	      this.lastIndex = totalRows ;
	    }
	  }



	public String getOrderBy() {
		return orderBy;
	}



	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}



	public String getSort() {
		return sort;
	}


	public void setSort(String sort) {
		this.sort = sort;
	}

	  
}
